package com.gongjian.database.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gongjian.database.entity.Classes;
import com.gongjian.utile.CodeEntity;
import com.gongjian.utile.utile.Hump;
import com.gongjian.utile.utile.Staic;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
    // SQL查询班级信息
public class ClassService {
    @Autowired
    SqlSession sqlSession;
    public String findAllClasse(Integer Page, Integer PageSzie, Classes classes){
        Staic staic = new Staic();
        //1、设置分页信息，包括当前页数和每页显示的总计数
        PageHelper.startPage(Page,PageSzie);
        //2、执行查询
        List<Class> classList = sqlSession.selectList("findAllClasse",classes);
        //3、获取分页查询后的数据
        PageInfo<Class> pageInfo = new PageInfo<>(classList);
        long page1 = pageInfo.getTotal();
        CodeEntity codeEntity = new CodeEntity(page1,200, "OK");//固定格式
        JSONArray j = JSONArray.parseArray(JSON.toJSONString(classList));//将list转换成JsonArray格式
        codeEntity.setData(j);//添加进json
        return codeEntity.toJSONString();
    }

    // 根据班级ID查看该班级的学生
    public String findUserClasse(Integer Page, Integer PageSzie, Classes classes){
        System.out.println(Page+"_"+PageSzie);
        Staic staic = new Staic();
        //1、设置分页信息，包括当前页数和每页显示的总计数
     //   PageHelper.startPage(Page,PageSzie);
        //2、执行查询
        List<Map<String,Object>> classList = sqlSession.selectList("findUserClasseMap",classes);
        //3、获取分页查询后的数据
     //   PageInfo<Class> pageInfo = new PageInfo<>(classList);
     //   long page1 = pageInfo.getTotal();
        CodeEntity codeEntity = new CodeEntity(200, "OK");//固定格式
        System.out.println(classList.size());
        JSONArray j = JSONArray.parseArray(JSON.toJSONString(Hump.formatHumpNameForList(classList)));//将list转换成JsonArray格式
        codeEntity.setData(j);//添加进json
        System.out.println(j.toString());
        return codeEntity.toJSONString();
    }

    //给班级添加单个学生(中间表)
    public String addUsersClass(Classes classes) {
        System.out.println(classes.getClasseId());
        System.out.println(classes.getUserId());
        List<Class> classList = sqlSession.selectList("addUsersClass",classes);
        CodeEntity codeEntity = new CodeEntity(200, "OK");//固定格式
        sqlSession.commit(true);
        return codeEntity.toJSONString();
    }

    //根据班级ID修改班级编号或者辅导员
    public String updateClass(Classes classes) {
        System.out.println(classes+"11111111");
        List<Class> classList = sqlSession.selectList("updateClass",classes);
        CodeEntity codeEntity = new CodeEntity(200, "OK");//固定格式
        sqlSession.commit(true);
        return codeEntity.toJSONString();
    }

    //添加班级
    public String addClass(Classes classes) {
        List<Class> classList = sqlSession.selectList("addClass",classes);
        CodeEntity codeEntity = new CodeEntity(200, "OK");//固定格式
        sqlSession.commit(true);
        return codeEntity.toJSONString();
    }

    //删除班级及中间表
    public String deleteClass(int[] ids) {
        for (int i : ids) {
            sqlSession.update("deleteClass", i);
        }
        sqlSession.commit();
        return new CodeEntity(200, "ok").toString();
    }


    public String deleteClasseUser(int userId) {
        sqlSession.update("deleteClasseUser", userId);
        sqlSession.commit();
        return new CodeEntity(200, "ok").toString();
    }
}
